/*
 * file name:mal_service.h
 * version :v1.0.0
 * Change Logs:
 * Date             Author      	main change
 * 2024-11-03     	lanxin          the first version
 *
*/

/********************************************* READ ME BEGIN *************************************************************/

/* Brief:
 *
 * * 1.用于解耦驱动与应用的中间层，其实现类似于 rt_device。
 *
*/

/********************************************* READ ME END ***************************************************************/

#ifndef _mal_service_H
#define _mal_service_H

/* source file */
#include "rtthread.h"
#include "service_cfg.h"

/* public define */
#define SERVICE_NAME_MAX    32

#ifndef SRV_PUT_TO_RAM 
#define SRV_PUT_TO_RAM
#endif

/* variable typedef */
typedef struct service s_service_t;
typedef int ( *usr_cb_t ) ( const s_service_t *srv, int event, const void *argv );

struct service
{
	rt_list_t list_srv;

	char name[SERVICE_NAME_MAX];

	void *usr_data;//save some para for application or low level,mal_service does not use it.

	usr_cb_t usr_cb;//usr_cb(srv,event,argv),decide by low level, argv can be usr_data or others. 

	int ( *init ) ( const struct service *srv, void *argv );// 0 succeed others failed,argv not save to mal_service
	int ( *stop ) ( const struct service *srv );// 0 succeed others failed
	int ( *read ) ( const struct service *srv, int pos, void *p_read, int read_size );//return read size,0 is read failed
	int ( *write ) ( const struct service *srv, int pos, const void *p_write, int write_size );//return write size,0 is write failed
	int ( *control ) ( const struct service *srv, int cmd, void *argv );
};

/* function typedef */

/* variable declaration for other to use */

/* function declaration for other to use */
SRV_PUT_TO_RAM const s_service_t *mal_service_find ( const char *name );
SRV_PUT_TO_RAM int mal_service_init ( const s_service_t *srv, void *argv );
SRV_PUT_TO_RAM int mal_service_init_by_name ( const char *name, void *argv );
SRV_PUT_TO_RAM int mal_service_read ( const s_service_t *srv, int pos, void *p_read, int read_size );
SRV_PUT_TO_RAM int mal_service_read_by_name ( const char *name, int pos, void *p_read, int read_size );
SRV_PUT_TO_RAM int mal_service_write ( const s_service_t *srv, int pos, const void *p_write, int write_size );
SRV_PUT_TO_RAM int mal_service_write_by_name ( const char *name, int pos, const void *p_write, int write_size );
SRV_PUT_TO_RAM int mal_service_stop ( const s_service_t *srv );
SRV_PUT_TO_RAM int mal_service_stop_by_name ( const char *name );
SRV_PUT_TO_RAM void mal_service_stop_all ( void );
SRV_PUT_TO_RAM int mal_service_control ( const s_service_t *srv, int cmd, void *argv );
SRV_PUT_TO_RAM int mal_service_control_by_name ( const char *name, int cmd, void *argv );
SRV_PUT_TO_RAM void mal_service_set_usr_data ( const s_service_t *srv, void *usr_data );
SRV_PUT_TO_RAM void mal_service_set_usr_data_by_name ( const char *name, void *usr_data );
SRV_PUT_TO_RAM void mal_service_set_usr_cb ( const s_service_t *srv, usr_cb_t usr_cb );
SRV_PUT_TO_RAM void mal_service_set_usr_cb_by_name ( const char *name, usr_cb_t usr_cb );
int mal_service_register ( s_service_t *srv );

#endif /* _mal_service_H */
